import { director, find, instantiate, Node, Vec3, _decorator } from "cc";

// import { UnityEngine } from "csharp";

import { BindNode } from "./BindNode";
import { Vector3 } from "./Vec";
import Util from "../Framework/Util/Util";

// const { ccclass, property } = _decorator;
// type GameObject = UnityEngine.GameObject;
// @ccclass
/**
 * 这里传进来不能再调用BINDENODE的函数，不然就会变成递归循环了导致报错
 */
export default class NodeHelper {

    public static FindChild(bindNode: BindNode, findName: string)
    {
        return bindNode.uObj.getChildByName(findName);
    }
    
    
    public static SetNodeName(bindeNode:BindNode, val: string)
    {
        bindeNode.uObj.name = val;
    }
  
    public static NewBindNode(name: string)
    {
        let go = new Node(name);
        return new BindNode(go);
    }

    /**
     *  从场景上查找节点
     */
    public static FindBindNode(name:string):BindNode
    {
        
        let go = find(name);
        if (go == null)
        {
            console.warn("FindNode node null",name);            
            return null;
        }

        return new BindNode(go);

    }

    public static Instantiate(bindeNode:BindNode):BindNode
    {
        var go = instantiate(bindeNode.uObj) as Node;
        director.getScene().addChild(go);
        return new BindNode(go);
    }

    public static SetPosition(node:BindNode,v3:Vector3)
    {
        node.uObj.position = new Vec3(v3.x,v3.y,v3.z);
    }

    public static SetActive(node:BindNode,state:boolean)
    {
        node.uObj.active = state;
    }


    public static SetParent(node:BindNode,parent:BindNode,stayWorldPos:boolean = false)
    {
        if (parent == null)
        {
            Util.Warn("SetParent null",node.uObj.name);
            // node.uObj.setParent(null,stayWorldPos);   
            director.getScene().addChild(node.uObj);
            return ;
        }
        node.uObj.setParent(parent.uObj,stayWorldPos);      
    }

    public static GetComponent(bindeNode:BindNode,typeCom:string)
    {
        return  bindeNode.uObj.getComponent(typeCom);
    }

    public static Destory(bindNode:BindNode)
    {
      bindNode.uObj.destroy();
    }

}